/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers.utils;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import models.Course;
import models.Department;
import models.Institute;
import models.StudyProgram;
/**
 *
 * @author dybvik
 */
@SessionScoped
@Named(value="myCourses")
public class myCourses implements Serializable{
    private final List<Course> courses = new ArrayList<Course>();
    private final DatabaseHjelper dbh = DatabaseHjelper.hent();
    
    public myCourses() {
        if(courses.isEmpty()){
            updateCourses();
        }
    }

    public String navToMyCourses(){
        updateCourses();
        return "myCourses";
    }

    private void updateCourses() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet res = null;
        try {
            conn = dbh.finnForbindelse();
            stmt = conn.prepareStatement("select course.*,studyprogram.*, department.*, instituteName, instPhoneNr, instNote"
                +" FROM (Studyprogram INNER JOIN (Course INNER JOIN studyprogramcourse ON course.COURSEID = studyprogramcourse.COURSEID)"
                +" ON Studyprogram.STUDYPROGRAMID = studyprogramcourse.STUDYPROGRAMID)"
                +" INNER JOIN (institute INNER JOIN (Department INNER JOIN studyprogramdepartment ON department.DEPARTMENTID = studyprogramdepartment.DEPARTMENTID)"
                +" ON institute.INSTITUTEID = department.INSTITUTEID) ON Studyprogram.STUDYPROGRAMID = studyprogramdepartment.STUDYPROGRAMID");
            res = stmt.executeQuery();
            synchronized (courses) {
                courses.clear();
                while (res.next()){
                    Course c = new Course();
                    c.setCourseID(res.getInt("courseID"));
                    c.setCourseCode(res.getString("courseCode"));
                    c.setCourseName(res.getString("courseName"));
                    c.setCourseLevel(res.getString("courseLevel"));
                    c.setCourseCredit(res.getString("courseCredits"));
                    c.setCredOverlapReduction(res.getString("credOverlapReduction"));
                    c.setSemester(res.getString("semester"));
                    c.setTeachingLanguage(res.getString("teachingLanguage"));
                    c.setPrerequisites(res.getString("prerequisites"));
                    c.setCourseContent(res.getString("courseContent"));
                    c.setPedMethods(res.getString("pedMethods"));
                    c.setAssessment(res.getString("assessment"));
                    c.setGradingScale(res.getString("gradingScale"));
                    c.setUseOfExaminers(res.getString("useofexaminers"));
                    c.setReqResitExamn(res.getString("reqResitExam"));
                    c.setExamAids(res.getString("examAids"));
                    c.setMandetoryWorkReq(res.getString("mandetoryWorkReq"));
                    c.setReadingList(res.getString("readingList"));
                    c.setReplacesCourse(res.getString("replacesCourse"));
                    c.setAdditionalNotes(res.getString("additionalNotes"));

                    //StudyProgram sP = new StudyProgram();
                    //sP.setStudyProgramID(res.getInt("studyProgramID"));
                    //sP.setStudyProgramCode(res.getInt("studyProgramCode"));
                    //sP.setName(res.getString("studyProgramName"));
                    //sP.setApproved(res.getDate("approved"));
                    //sP.setLastRevised(res.getDate("lastRevised"));
                    //sP.setIsNKRFromInt(res.getInt("isNKR"));
                    //sP.setApprovedBy(res.getString("approvedBy"));

                    //Department dep = new Department();
                    //dep.setDepartmentID(res.getInt("departmentID"));
                    //dep.setNavn(res.getString("departmentName"));
                    //dep.setPhoneNr(res.getString("depPhoneNr"));
                    //dep.setNote(res.getString("depNote"));

                    //Institute inst = new Institute();
                    //inst.setInstituteID(res.getInt("instituteID"));
                    //inst.setName(res.getString("instituteName"));
                    //inst.setPhoneNr(res.getString("instPhoneNr"));
                    //inst.setNote(res.getString("instNote"));

                    //dep.setInstitute(inst);
                    //sP.setDepartment(dep);
                    //c.setStudyprogram(sP);
                    System.out.println("Dette er i obj.: ID: " + c.getCourseID()+" Code: "+c.getCourseCode()+" Name: "+c.getCourseName());
                    courses.add(c);                         
                }
                System.out.println("Størrelsen på courses: " + courses.size());
            }
          //nav = "myCourses";
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            DatabaseHjelper.lukk(res);
            DatabaseHjelper.lukk(stmt);
            DatabaseHjelper.lukk(conn);
            //return nav;
        }
    }

    public List<Course> getCourses() {
        return courses;
    }
    
}


